package com.yimi.yinhepda.views.seal_module;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.yimi.yinhepda.R;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.entity.request.SealManagerRequest;
import com.yimi.yinhepda.entity.request.SealUploadRequest;
import com.pda.ym.entity.response.NoDataResponse;
import com.yimi.yinhepda.entity.response.SealDownloadResponse;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.uce.commom.util.MyDialog;
import com.pda.ym.utils.SharedPreferUtil;
import com.pda.ym.utils.StringUtils;
import com.yimi.yinhepda.base.BaseInitActivity;
import com.pda.ym.utils.SoundVibratorManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 封签管理页面
 *
 * @author tianjin
 */
public class SealManagerActivity extends BaseInitActivity {

  private TextView mTvSealCarCode, mTvCarNo, mTvSealNo1, mTvSealNo2, mTvSealNo3, mTvSealNo4, mTvSealNo5, mTvSealNo6;
  ;
  /** 根据网点和配载单号返回的封签号的集合，来和输入的签号进行判断的 */
  private List<String> responseSealNos = new ArrayList<>();
  private Boolean isDownloadSuccess = false;
  private List<String> mSealList;

  @Override public String getPageName() {
    return getString(R.string.page_seal_manager);
  }

  public SealManagerActivity() {
    super(true);
  }

  @Override public int getContentViewId() {
    return R.layout.activity_seal_manager;
  }

  @Override public void initView(Bundle savedInstanceState) {
    ((TextView) findViewById(R.id.title)).setText("封签管理");
    //配置单号
    mTvSealCarCode = (TextView) findViewById(R.id.edit_seal_carCode);
    //车牌号
    mTvCarNo = (TextView) findViewById(R.id.seal_carNo);
    //封签1
    mTvSealNo1 = (TextView) findViewById(R.id.seal_no1);
    //封签2
    mTvSealNo2 = (TextView) findViewById(R.id.seal_no2);
    //封签3
    mTvSealNo3 = (TextView) findViewById(R.id.seal_no3);
    //封签4
    mTvSealNo4 = (TextView) findViewById(R.id.seal_no4);
    //封签5
    mTvSealNo5 = (TextView) findViewById(R.id.seal_no5);
    //封签6
    mTvSealNo6 = (TextView) findViewById(R.id.seal_no6);
  }

  @Override public void initEvent() {
    findViewById(R.id.deblocking_btn).setOnClickListener(this);
    findViewById(R.id.back_btn).setOnClickListener(this);
    findViewById(R.id.btn_seal_download).setOnClickListener(this);
  }

  @Override public void initData() {
  }

  /**
   * 扫描回调
   */
  @Override protected void handleBarCode(String result) {
    super.handleBarCode(result);

    if (mTvSealCarCode.isFocused()) {
      mTvSealCarCode.setText(result);
      return;
    }
    if (mTvSealNo1.isFocused()) {
      mTvSealNo1.setText(result);
      return;
    }
    if (mTvSealNo2.isFocused()) {
      mTvSealNo2.setText(result);
      return;
    }
    if (mTvSealNo3.isFocused()) {
      mTvSealNo3.setText(result);
      return;
    }
    if (mTvSealNo4.isFocused()) {
      mTvSealNo4.setText(result);
      return;
    }
    if (mTvSealNo5.isFocused()) {
      mTvSealNo5.setText(result);
      return;
    }
    if (mTvSealNo6.isFocused()) {
      mTvSealNo6.setText(result);
    }
  }

  @Override public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
      case R.id.back_btn:
        finish();
        break;
      case R.id.btn_seal_download:
        //下载按钮
        startDownload();
        //                test();
        break;
      case R.id.deblocking_btn:
        //解封按钮
        final String carCode = mTvSealCarCode.getText().toString();
        String mStrNo1 = mTvSealNo1.getText().toString();
        String mStrNo2 = mTvSealNo2.getText().toString();
        String mStrNo3 = mTvSealNo3.getText().toString();
        String mStrNo4 = mTvSealNo4.getText().toString();
        String mStrNo5 = mTvSealNo5.getText().toString();
        String mStrNo6 = mTvSealNo6.getText().toString();

        if (TextUtils.isEmpty(carCode)) {
          showToast("请输入配载单号!");
          return;
        }

        if (!isDownloadSuccess) {
          showToast("请下载配载信息!");
          return;
        }

        if (TextUtils.isEmpty(mStrNo1) && TextUtils.isEmpty(mStrNo2) && TextUtils.isEmpty(mStrNo3) && TextUtils.isEmpty(mStrNo4) && TextUtils.isEmpty(
            mStrNo5) && TextUtils.isEmpty(mStrNo6)) {
          showToast("封签码至少一个不为空!");
          return;
        }

        //扫描到的封签信息中只要有一个不再返回的封签码列表里，就提示
        deblockingNew();
        break;
      default:
        break;
    }
  }

  public void test() {
    isDownloadSuccess = true;
    mTvCarNo.setText("7868687");
    responseSealNos.add("100");
    responseSealNos.add("200");
    //        responseSealNos.add("300");
  }

  /**
   * 点击下载按钮，根据配置单号
   */
  public void startDownload() {

    String stowageNo = mTvSealCarCode.getText().toString();
    stowageNo = stowageNo.toUpperCase();
    mTvSealCarCode.setText(stowageNo);
    if (!StringUtils.isCarCodeNo(stowageNo)) {
      showToast("输入正确的配载单号!");
      return;
    }
    showPgDlg("正在加载...");
    SealManagerRequest request = new SealManagerRequest();
    request.setSrcDeptCode(sharedPreferUtil.getDeptCode());
    request.setTaskNo(stowageNo);

    new NetRequest().setMethod(NetRequest.Method.POST_STRING)
        .setUrl(Constants.URL_V2_QUERY_SEAL_INFO)
        .setRequestObject(request)
        .setResponsClazz(SealDownloadResponse.class)
        .execute(new UICallBack() {
          @Override public void onError(String msg) {
            if (msg != null) {
              showToast(msg);
            } else {
              showToast("服务器出错了!");
            }
            SoundVibratorManager.playSound(2);
            dismissPgDlg();
            isDownloadSuccess = false;
          }

          @Override public void onSuccess(Object obj) {
            SealDownloadResponse response = (SealDownloadResponse) obj;

            if (response.getData() == null) {
              SoundVibratorManager.playSound(2);
              if (response.getErrorMessages() != null && response.getErrorMessages().length > 0) {
                showToast(response.getErrorMessages()[0]);
                SoundVibratorManager.playSound(2);
              } else {
                showToast("封签数据下载失败!");
                SoundVibratorManager.playSound(2);
              }
              dismissPgDlg();
              isDownloadSuccess = false;
              return;
            }

            isDownloadSuccess = true;

//            mTvCarNo.setText(response.getData().getCarNo());
//
//            if (response.getData().getSealNos().size() == 6) {
//              String seal1 = response.getData().getSealNos().get(0);
//              if (!TextUtils.isEmpty(seal1)) {
//                responseSealNos.add(seal1);
//              }
//
//              String seal2 = response.getData().getSealNos().get(1);
//              if (!TextUtils.isEmpty(seal2)) {
//                responseSealNos.add(seal2);
//              }
//              String seal3 = response.getData().getSealNos().get(2);
//              if (!TextUtils.isEmpty(seal3)) {
//                responseSealNos.add(seal3);
//              }
//              String seal4 = response.getData().getSealNos().get(3);
//              if (!TextUtils.isEmpty(seal4)) {
//                responseSealNos.add(seal4);
//              }
//              String seal5 = response.getData().getSealNos().get(4);
//              if (!TextUtils.isEmpty(seal5)) {
//                responseSealNos.add(seal5);
//              }
//              String seal6 = response.getData().getSealNos().get(5);
//              if (!TextUtils.isEmpty(seal6)) {
//                responseSealNos.add(seal6);
//              }

//              Log.d("22", "onResponse: responseSealNos.size() = " + responseSealNos.size() + ",具体=" + responseSealNos.toString());
//
//              SoundVibratorManager.playSound(1);
//            } else {
//              showToast("服务器返回数据不正确！");
//              SoundVibratorManager.playSound(2);
//            }

            dismissPgDlg();
          }
        });
  }

  public void deblockingNew() {

    mSealList = new ArrayList<>();
    String sealNo1 = mTvSealNo1.getText().toString().trim();
    String sealNo2 = mTvSealNo2.getText().toString().trim();
    String sealNo3 = mTvSealNo3.getText().toString().trim();
    String sealNo4 = mTvSealNo4.getText().toString().trim();
    String sealNo5 = mTvSealNo5.getText().toString().trim();
    String sealNo6 = mTvSealNo6.getText().toString().trim();

    if (!TextUtils.isEmpty(sealNo1) && !sealNo1.equals("")) {
      mSealList.add(sealNo1);
    }
    if (!TextUtils.isEmpty(sealNo2) && !sealNo1.equals("")) {
      mSealList.add(sealNo2);
    }
    if (!TextUtils.isEmpty(sealNo3) && !sealNo1.equals("")) {
      mSealList.add(sealNo3);
    }
    if (!TextUtils.isEmpty(sealNo4) && !sealNo1.equals("")) {
      mSealList.add(sealNo4);
    }
    if (!TextUtils.isEmpty(sealNo5) && !sealNo1.equals("")) {
      mSealList.add(sealNo5);
    }
    if (!TextUtils.isEmpty(sealNo6) && !sealNo1.equals("")) {
      mSealList.add(sealNo6);
    }

    Log.d("22", "onResponse: mSealList.size() = " + mSealList.size() + ",具体=" + mSealList.toString());

    //输入的封签数量要和接口给的一样，还有内容也相同
    if (responseSealNos.size() == mSealList.size()) {
      if (mSealList.containsAll(responseSealNos)) {
        Log.d("tag", "正常解封: ");
        uplaodSealInfo();
      } else {
        Log.d("tag", "强制解封: ");
        forceSealDialg();
      }
    } else {
      //强制解封
      Log.d("tag", "强制解封: ");
      forceSealDialg();
    }
  }

  /**
   * 强制解封
   */
  public void forceSealDialg() {
    MyDialog dialog = new MyDialog(SealManagerActivity.this);
    dialog.setMessage("封签码信息不符或缺失，是否强制解封");
    dialog.setPositiveClickListener( Constants.confirmMsg, new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        //上传解封信息
        uplaodSealInfo();
      }
    });
    dialog.setNegativeClickListener(R.drawable.btn_cancel_selector, Constants.cancelMsg, new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
      }
    });
    dialog.show();
  }

  /**
   * 上传解封的数据，访问解封接口
   */
  public void uplaodSealInfo() {

    showPgDlg("正在上传...");
    SealUploadRequest sealUploadRequest = new SealUploadRequest();
    sealUploadRequest.setStowageNo(mTvSealCarCode.getText().toString().trim());
    sealUploadRequest.setSrcDeptCode(SharedPreferUtil.getInstance().getValue(Common.DEPT_CODE));

    sealUploadRequest.setSealNos(mSealList);

    new NetRequest().setMethod(NetRequest.Method.POST_STRING)
        .setUrl(Constants.URL_SEAL_UPLOAD)
        .setRequestObject(sealUploadRequest)
        .setResponsClazz(NoDataResponse.class)
        .execute(new UICallBack() {
          @Override public void onError(String msg) {
            if (msg != null) {
              showToast(msg);
            } else {
              showToast("服务器出错了!");
            }
            SoundVibratorManager.playSound(2);
            dismissPgDlg();
          }

          @Override public void onSuccess(Object obj) {
            showToast("解封成功!");
            SoundVibratorManager.playSound(1);
            dismissPgDlg();
            finish();
          }
        });
  }

  /**
   * 解封码的判断
   */
  //    public void deblocking() {
  //
  //        List<String> mSealList = new ArrayList<>();
  //        String sealNo1 = mTvSealNo1.getText().toString();
  //        String sealNo2 = mTvSealNo2.getText().toString();
  //        String sealNo3 = mTvSealNo3.getText().toString();
  //
  //        mSealList.add(sealNo1);
  //        mSealList.add(sealNo2);
  //        mSealList.add(sealNo3);
  //
  //        //根据输入的封签号来判断，只要输入的对的，就通过，如果输入的其中一个不对就是不对
  //        Iterator<String> iterator = mSealList.iterator();
  //        while (iterator.hasNext()){
  //            String sealNo = iterator.next();
  //            if(!TextUtils.isEmpty(sealNo)){
  //                if(!responseSealNos.contains(sealNo)){
  //
  //                    //这里dailog准备上传
  //                    isUpload = true;
  //
  //                    MyDialog dialog = new MyDialog(SealManagerActivity.this);
  //                    dialog.setMessage("封签码信息不符或缺失，是否强制解封");
  //                    dialog.setPositiveClickListener(R.drawable.btn_confirm_selector
  //                            , Constants.confirmMsg, new DialogInterface.OnClickListener() {
  //                                @Override
  //                                public void onClick(DialogInterface dialog, int which) {
  //                                    dialog.dismiss();
  //                                    //上传解封信息
  //                                    uplaodSealInfo();
  //                                }
  //                            });
  //                    dialog.setNegativeClickListener(R.drawable.btn_cancel_selector,
  //                            Constants.cancelMsg, new DialogInterface.OnClickListener() {
  //                                @Override
  //                                public void onClick(DialogInterface dialog, int which) {
  //                                    dialog.dismiss();
  //                                    //必须的
  //                                    isUpload = false;
  //                                }
  //                            });
  //                    dialog.show();
  //                    break;//只提示一次
  //                }
  //            }
  //        }
  //
  //        //开始访问接口
  //        if(!isUpload){
  //            Log.d("tag", "onClick: "+"正常上传");
  //            uplaodSealInfo();
  //        }
  //
  //    }
}
